<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header"><?php echo __('Product') ?></h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                
                <div class="panel-body">
                    <div class="row">
                        <?php
                            echo $this->Form->create('BoostCake', array(
                                'inputDefaults' => array(
                                    'div' => 'form-group',
                                    'wrapInput' => false,
                                    'class' => 'form-control',
                                    'method' => 'post'
                                ),
                                //'class' => 'well',
                                'enctype' => 'multipart/form-data'
                            ));
                            ?>
                        <div class="<?php echo $class_table ?>">
                            
                            <fieldset>
                                <legend>
                                    <?php echo __('Product') ?></legend>
                                <?php
                                echo $this->Form->input('Product.category_id', array(
                                    'label' => __('Select Category'),
                                    'type' => 'select',
                                    'options' => $categories,
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.product_type', array(
                                    'label' => false,
                                    'type' => 'hidden'
                                ));
                                ?>
                                
                                <?php
                                echo $this->Form->input('Product.price', array(
                                    'label' => __('Price'),
                                    'type' => 'text'
                                ));
                                ?>
                                 <?php
                                echo $this->Form->input('Product.unit_price', array(
                                    'label' => __('Unit Price'),
                                    'type' => 'select',
                                    'options' => array('1'=>'Price/Total Property','2'=>'Price/m2'),
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.location_id', array(
                                    'label' => __('Select Location'),
                                    'type' => 'select',
                                    'options' => $location,
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.address', array(
                                    'label' => __('Address'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.name', array(
                                    'label' => __('Product Name'),
                                    'type' => 'text'
                                ));
                                ?>
                                
                                <!---------------- image ---------------->
                                <div>
                                    <?php
                                    echo $this->Form->input('Product.image', array(
                                        'label' => __('File'),
                                        'type' => 'File',
                                        'required' => false,
                                        'src' => $this->webroot . '' . $base_config_url . '' . $name,
                                    ));
                                    echo '<img src="' . $this->webroot . $base_config_url . $name . '" style="width:100px;height:80px;">';
                                    ?>


                                    <div id="list_multiimg">
                                        <?php if(is_array($MultiImages)){foreach($MultiImages as $value_img){?>
                                        <div class="divimg_multi">
                                        <a class="condition_remove_img glyphicon glyphicon-remove"></a>
                                        <?php
                                            echo '<img title="'.$value_img['ProductImage']['url'].'" src="' . $this->webroot . $base_config_url . $value_img['ProductImage']['url'] . '" style="width:100px;height:80px;">';

                                        ?>
                                            <input type="hidden" value="<?php echo $value_img['ProductImage']['image_id']  ?>" name="ArrMultiImg[<?php echo $value_img['ProductImage']['image_id']  ?>]" />
                                        </div>    
                                        <?php }}?>
                                        <div class="clear"></div>
                                    </div>
                                    <?php $key = -1; ?>

                                    <div id="container_value" style="margin:10px 0px 10px 0px;">
                                        <div class="form-group" ></div>
                                    </div>
                                    <div class="btn" id="add_value"><a class="glyphicon glyphicon-plus"><?php echo __('Add'); ?></a></div>
                                </div>
                                <!------------------- end image ----------------->
                                
                                
                            </fieldset>
                        </div>
                        <!---------------- attribute ------------------->
                        <div class="col-lg-6">
                            <fieldset>
                                <legend><?php echo __('Attributes')?></legend>
                                <?php foreach($properties as $value):?>
                                    <!-- select -->
                                    <?php if($value['type'] == 1):?>
                                        <?php 
                                        echo $this->Form->input('ProductPropertyValue.'.$value['property_id'].'.property_value_id', 
                                            array(
                                                'label' => $value['name'],
                                                'type' => 'select',
                                                'options' => $value['values']
                                            ));
                                        ?>
                                    <?php endif;?>

                                    <!-- multiple select -->
                                    <?php if($value['type'] == 2):?>
                                        <?php 
                                        echo $this->Form->input('ProductPropertyValueArr.'.$value['property_id'].'.property_value', 
                                            array(
                                                'label' => $value['name'],
                                                'type' => 'select',
                                                'multiple'=>'checkbox',
                                                'options' => $value['values']
                                            ));
                                        ?>
                                    <?php endif;?>

                                    <!-- text box -->
                                    <?php if($value['type'] == 3):?>
                                        <?php 
                                        echo $this->Form->input('ProductPropertyValueText.'.$value['property_id'].'.property_value_language', 
                                            array(
                                                'label' => $value['name'],
                                                'type' => 'text',
                                            ));
                                        ?>
                                    <?php endif;?>

                                <?php endforeach;?>
                            </fieldset>
                        </div>
                        <!------------------ end attribute --------------->
                        <!------------------ contact ----------------->
                        <div class="col-lg-6">
                            <fieldset>
                                <legend><?php echo __('Contact')?></legend>
                            
                                
                                <?php
                                echo $this->Form->input('Product.contact_email', array(
                                    'label' => __('Contact email'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.contact_phone', array(
                                    'label' => __('Contact phone'),
                                    'row' => '5'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.contact_name', array(
                                    'label' => __('Contact mobile'),
                                    'type' => 'text'
                                ));
                                ?>
                             </fieldset>
                        </div>
                        <!--------------------------- end contact ------------------->
                        <!------------------ seo ----------------->
                        <div class="col-lg-6">
                            <fieldset>
                                <legend><?php echo __('SEO')?></legend>
                            
                                <?php
                                echo $this->Form->input('Productlanguage.seo_title', array(
                                    'label' => __('SEO').' '.__('Title'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.seo_keyword', array(
                                    'label' => __('SEO').' '.__('Key word'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.seo_description', array(
                                    'label' => __('SEO').' '.__('Description'),
                                    'row' => '5'
                                ));
                                ?>
                             </fieldset>
                        </div>
                        <!--------------------------- end seo ------------------->
                        <!-- suggestion product -->
                        <div class="col-lg-12" id="container_autocomplete">
                            <div class="form-group" style="position: relative" >
                                <label >Add room</label>
                                <input type="text" id="autocomplete" class="form-control"/>
                            </div>
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th><?php echo __('Id Room')?></th>
                                        <th><?php echo __('Name Room')?></th>
                                        <th><?php echo __('Action')?></th>
                                    </tr>
                                </thead>
                                <tbody id="container_suggesstion_list">
                                    <?php foreach($products as $item):?>
                                        <tr>
                                            <td><?php echo $item['Suggestion']['child']?></td>
                                            <td><?php echo $item['Productlanguage']['name']?></td>
                                            <td><div class="btn btn-danger delete_suggestion" data-id="<?php echo $item['Suggestion']['id']?>"><?php echo __('delete');?></div></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <!-- end suggestion product -->
                        <!------------- description -------------------->
                        <div class="col-lg-12">
                            <?php
                                echo $this->Form->input('Productlanguage.description', array(
                                    'label' => __('Product Description'),
                                    'row' => '10',
                                    'class' => 'ckeditor',
                                ));
                                ?>
                        </div>
                        <!------------- end description -------------------->
                        <div class="col-lg-12">
                            <?php
                                echo $this->Form->submit('Save', array(
                                    'div' => 'form-group',
                                    'class' => 'btn btn-danger'
                                ));
                                ?>
                        </div>
                        <div style="clear: both"></div>
                        
                     	<?php echo $this->Form->end(); ?>
                    </div>
                    

                </div>
                
                       
            </div>
            <!-- /.row (nested) -->
        </div>
        <!-- /.panel-body -->
    </div>
    <!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
</div>


<?php echo $this->Html->script('ckeditor/ckeditor');  ?>

<script>
            
    $(document).ready(function(){
        var key = <?php echo $key;?>;
        var html = '<div class="group_value_property">\n\
                        <a class="condition_remove glyphicon glyphicon-remove"></a>\n\
                            <input type="file" name="data[multiImages][key][value]" class="form-control">\n\
                    </div>';
        $("#add_value").on('click',function(){
           key = key+1;
           var add = html.replace(new RegExp("key", 'g'),key);
           $("#container_value .form-group").last().after(add);
        });
        
        $(document).on('click','.condition_remove',function(){
            var div_parent = $(this).parents('.group_value_property');
            div_parent.remove();
	});
        
        $(document).on('click','.condition_remove_img',function(){
            var div_parent = $(this).parents('.divimg_multi');
            var input_hideen = div_parent.find( 'input[type=hidden]');
            if(input_hideen.size() == 1){
                
                console.log(input_hideen.val());
                $.ajax({
                    url: '<?php echo $this->Html->url('/product/delete')?>',
                    data: {'id':input_hideen.val()},
                    type: 'post',
                    dataType: 'json',
                    success: function(data){
                        if(data.success){
                            div_parent.remove();
                        }
                    }
                });
                
            }else{
                div_parent.remove();
            }
	});
        
        productId = <?php echo $parentsId ?>;
        $('#autocomplete').typeahead({
            
            onSelect: function(item) {
                console.log(item);
                var r = confirm("do you want to add "+item.text+" to this product");
                if(r){
                    $.ajax({
                        url: "<?php echo $this->Html->url('/product/suggestion')?>",
                        data:{'child':item.value,'parents':productId},
                        type: 'post',
                        success: function(data){
                            if(data.length > 0){
                                $("#container_suggesstion_list").html(data);
                            }
                        }
                    });
                }
            },
            ajax: {
                url: "<?php echo $this->Html->url('/product/ajax')?>",
                timeout: 500,
                displayField: 'name',
                triggerLength: 1,
                method: "post",
                loadingClass: "loading-circle",
                preDispatch: function (query) {
                    return {
                        search: query
                    }
                },
                preProcess: function (data) {
                    return data;
                }
             }
            
       });
       
       //delete suggestion
       $(document).on('click','.delete_suggestion',function(){
            item = $(this);
            $.ajax({
                        url: "<?php echo $this->Html->url('/product/remove_suggestion')?>",
                        data:{'id':item.data('id')},
                        type: 'post',
                        success: function(data){
                            item.parents('tr').remove();
                        }
            });
       });
          
          
    });
</script>